//
//  LocationModel.m
//  BrotherMovProject
//
//  Created by zhibao on 2022/1/19.
//

#import "LocationModel.h"
@interface LocationModel(){
    
}
@end
@implementation LocationModel

- (instancetype)init{
    self = [super init];
    if (self) {
        _isTift = @"";
        _cityCode = @"";
        _cityID = @"";
        _provinceCode = @"";
        _addInfo = @"";
        _lon = 0.00;
        _numberPlate = @"";
        _floorContent = @"";
        _areaCode = @"";
        _contactsPhone = @"";
        _cityName = @"";
        _areaName = @"";
        _evident = @"";
        _moveAddressType = @"";
        _floorName = @"";
        _floorPrice = @"";
        _provinceName = @"";
        _lat = 0.00;
        _POIName = @"";
    }
    return self;
}
-(id)copyWithZone:(NSZone *)zone
{
    NSLog(@"自定义对象拷贝");

    //allocWithZone: 开辟zone大小的空间
    LocationModel *xiaoM = [LocationModel allocWithZone:zone];
    xiaoM.cityCode = self.cityCode;
    xiaoM.cityID = self.cityID;
    xiaoM.provinceCode = self.provinceCode;
    xiaoM.addInfo = self.addInfo;
    xiaoM.lon = self.lon;
    xiaoM.numberPlate = self.numberPlate;
    xiaoM.floorContent = self.floorContent;
    xiaoM.areaCode = self.areaCode;
    xiaoM.contactsPhone = self.contactsPhone;
    xiaoM.cityName = self.cityName;
    xiaoM.areaName = self.areaName;
    xiaoM.evident = self.evident;
    xiaoM.moveAddressType = self.moveAddressType;
    xiaoM.floorName = self.floorName;
    xiaoM.floorPrice = self.floorPrice;
    xiaoM.provinceName = self.provinceName;
    xiaoM.lat = self.lat;
    xiaoM.POIName = self.POIName;
    return xiaoM;
}


-(id)mutableCopyWithZone:(NSZone *)zone
{
    NSLog(@"自定义对象的mutable拷贝");

    LocationModel *xiaoM = [LocationModel allocWithZone:zone];
    xiaoM.cityCode = self.cityCode;
    xiaoM.cityID = self.cityID;
    xiaoM.provinceCode = self.provinceCode;
    xiaoM.addInfo = self.addInfo;
    xiaoM.lon = self.lon;
    xiaoM.numberPlate = self.numberPlate;
    xiaoM.floorContent = self.floorContent;
    xiaoM.areaCode = self.areaCode;
    xiaoM.contactsPhone = self.contactsPhone;
    xiaoM.cityName = self.cityName;
    xiaoM.areaName = self.areaName;
    xiaoM.evident = self.evident;
    xiaoM.moveAddressType = self.moveAddressType;
    xiaoM.floorName = self.floorName;
    xiaoM.floorPrice = self.floorPrice;
    xiaoM.provinceName = self.provinceName;
    xiaoM.lat = self.lat;
    xiaoM.POIName = self.POIName;
    return xiaoM;
}
//模型转字典
- (NSDictionary *)dicFromObject:(NSObject *)object {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    unsigned int count;
    objc_property_t *propertyList = class_copyPropertyList([object class], &count);
    
    for (int i = 0; i < count; i++) {
        objc_property_t property = propertyList[i];
        const char *cName = property_getName(property);
        NSString *name = [NSString stringWithUTF8String:cName];
        NSObject *value = [object valueForKey:name];//valueForKey返回的数字和字符串都是对象
        
        if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSNumber class]]) {
            //string , bool, int ,NSinteger
            [dic setObject:value forKey:name];
            
        } else if ([value isKindOfClass:[NSArray class]]) {
            //数组或字典
            [dic setObject:[self arrayWithObject:value] forKey:name];
        } else if ([value isKindOfClass:[NSDictionary class]]) {
            //数组或字典
            [dic setObject:[self dicWithObject:value] forKey:name];
        } else if (value == nil) {
            //null
            //[dic setObject:[NSNull null] forKey:name];//这行可以注释掉?????
        } else {
            //model
            [dic setObject:[self dicFromObject:value] forKey:name];
        }
    }
    
    return [dic copy];
}

//转数组
- (NSArray *)arrayWithObject:(id)object {
    //数组
    NSMutableArray *array = [NSMutableArray array];
    NSArray *originArr = (NSArray *)object;
    if ([originArr isKindOfClass:[NSArray class]]) {
        for (NSObject *object in originArr) {
            if ([object isKindOfClass:[NSString class]]||[object isKindOfClass:[NSNumber class]]) {
                //string , bool, int ,NSinteger
                [array addObject:object];
            } else if ([object isKindOfClass:[NSArray class]]) {
                //数组或字典
                [array addObject:[self arrayWithObject:object]];
            } else if ([object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [array addObject:[self dicWithObject:object]];
            } else {
                //model
                [array addObject:[self dicFromObject:object]];
            }
        }
        return [array copy];
    }
    return array.copy;
}

//转字典类型
- (NSDictionary *)dicWithObject:(id)object {
    //字典
    NSDictionary *originDic = (NSDictionary *)object;
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if ([object isKindOfClass:[NSDictionary class]]) {
        for (NSString *key in originDic.allKeys) {
            id object = [originDic objectForKey:key];
            if ([object isKindOfClass:[NSString class]]||[object isKindOfClass:[NSNumber class]]) {
                //string , bool, int ,NSinteger
                [dic setObject:object forKey:key];
            } else if ([object isKindOfClass:[NSArray class]]) {
                //数组或字典
                [dic setObject:[self arrayWithObject:object] forKey:key];
            } else if ([object isKindOfClass:[NSDictionary class]]) {
                //数组或字典
                [dic setObject:[self dicWithObject:object] forKey:key];
            } else {
                //model
                [dic setObject:[self dicFromObject:object] forKey:key];
            }
        }
        return [dic copy];
    }
    return dic.copy;
}
@end
